package com.itheima.aop;

import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LiangTianQi
 * @describe
 * @date 2020/11/16 10:41
 */
public class AopAdvice {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Value("${out_put_path}")
    private String outPutPath;


    //套餐变更获取静态页面
    public void generateMobileStaticHtml() {
        //从数据库中全部套餐信息
        List<Setmeal> list = setmealDao.getAll();
        //调用获取静态列表页面
        generateMobileSetmealListHtml(list);

        //循环遍历list从中获取套餐的id
        for (int i = 0; i < list.size(); i++) {
            Integer id = list.get(i).getId();
            //从数据库查询单个套餐的信息包括检查组和检查项
            Setmeal setmeal = setmealDao.getByIdMany(id);
//            System.out.println(setmeal);
            //判断项目组是否有值
//            System.out.println(setmeal.getCheckGroups());
            if (setmeal.getCheckGroups() !=null) {
                generateMobileSetmealDetailHtml(setmeal);
            }
        }
    }

    //检查组更新获取静态页面
    public void generateMobileCheckGroupStaticHtml(CheckGroup checkGroup) {
        Integer checkGroupId = checkGroup.getId();
        List<Integer> ids = checkGroupDao.getSetmealIdByCheckGroupId(checkGroupId);
        if (ids != null) {
            //循环遍历list从中获取套餐的id
            for (int i = 0; i < ids.size(); i++) {
                Integer id = ids.get(i);
                //从数据库查询单个套餐的信息包括检查组和检查项
                Setmeal setmeal = setmealDao.getByIdMany(id);
                //判断项目组是否有值
                if (setmeal.getCheckGroups() !=null) {
                    generateMobileSetmealDetailHtml(setmeal);
                }
            }
        }
    }


    //检查项变更获取静态页面
    public void generateMobileCheckItemStaticHtml(CheckItem checkItem) {
        Integer checkItemId = checkItem.getId();
        List<Integer> checkGroupIDByIds = checkItemDao.getCheckGroupIDById(checkItemId);
        if (checkGroupIDByIds != null) {
            for (int i = 0; i < checkGroupIDByIds.size(); i++) {
                CheckGroup checkGroup = checkGroupDao.get(checkGroupIDByIds.get(i));
                generateMobileCheckGroupStaticHtml(checkGroup);
            }
        }
    }


    //获取静态列表页面
    public void generateMobileSetmealListHtml(List list) {
        //将集合封装成map
        Map map = new HashMap();
        map.put("setmealList", list);
        generateHtml("mobile_setmeal.ftl","m_setmeal.html",map);
    }

    //获取静态详情页面
    public void generateMobileSetmealDetailHtml(Setmeal setmeal) {
        //将对象封装成map
        Map map = new HashMap();
        map.put("setmeal", setmeal);
        generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+setmeal.getId()+".html",map);
    }





    //生成静态页面的基本模板
    public void generateHtml(String templateName, String htmlPageName, Map map) {
        //加载模板所在的文件夹
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Writer out=null;
        try {
            //获得模板对象
            Template template = configuration.getTemplate(templateName);
            //获取生成文件的输出流
            out = new FileWriter(new File(outPutPath+"\\"+htmlPageName));
            template.process(map,out);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
